<?php
RegisterPlugin("ytecn_pjax","ActivePlugin_ytecn_pjax");
function ActivePlugin_ytecn_pjax() {      		 			
    global $zbp;        			 
    Add_Filter_Plugin('Filter_Plugin_Admin_TopMenu', 'ytecn_pjax_Admin_SiteInfo_SubMenu');
    Add_Filter_Plugin('Filter_Plugin_Edit_Response5','ytecn_pjax_edit_response');
    Add_Filter_Plugin('Filter_Plugin_Post_Get', 'ytecn_pjax_post_get');
    Add_Filter_Plugin('Filter_Plugin_PostArticle_Core', 'ytecn_pjax_PostArticle_Core');
    Add_Filter_Plugin('Filter_Plugin_Zbp_MakeTemplatetags','ytecn_pjax_set');
    Add_Filter_Plugin('Filter_Plugin_PostArticle_Succeed','ytecn_pjax_PostArticle_Succeed');       			 	
    Add_Filter_Plugin('Filter_Plugin_Cmd_Ajax', 'ytecn_pjax_Cmd_Ajax');      			   
    if($zbp->Config('ytecn_pjax')->seozdy){     		 				
        Add_Filter_Plugin('Filter_Plugin_Category_Edit_Response','ytecn_pjax_Category_Edit_Response');     			 			
        Add_Filter_Plugin('Filter_Plugin_Tag_Edit_Response','ytecn_pjax_Tag_Edit_Response');
        Add_Filter_Plugin('Filter_Plugin_Member_Edit_Response','ytecn_pjax_Member_Edit_Response');       					
    }          	 
}    	     	 
    	   	  	
function ytecn_pjax_Cmd_Ajax($src)     	 	 	 	
{    			 				
    global $zbp;    	 		  	 
    if ($src == 'ytecn_pjax') {     	   		 
        if (!$zbp->CheckRights('UploadPst')) {    			     
            $zbp->ShowError(6);    	  	 		 
        }    		  	 	 
        Add_Filter_Plugin('Filter_Plugin_Upload_SaveFile', 'ytecn_pjax_Upload_SaveFile_Ajax');    	 	    	
        $_POST['auto_rename'] = 1;
        PostUpload();
        echo json_encode(array('url' => $GLOBALS['tmp_ul']->Url));    			  			
        exit;    		   	  
    }    	 					 
}      				 	
     	 		  	
function ytecn_pjax_Upload_SaveFile_Ajax($tmp, $ul)     	      
{     			    
    $GLOBALS['tmp_ul'] = $ul;    		   	 	
}    		  	   
    	 	   		
function ytecn_pjax_PostArticle_Succeed() {    		 	  		
    global $zbp;
    $sql = $zbp->db->sql->get()      	   	 
    ->select($zbp->table['Post'])    	  			  
    ->where(array('=', 'log_Type', "0"))    	 	  	 	
    ->where(array('=', 'log_Status', "0"))     	  				
    ->count(array('log_ID'=>'num'))      	 	  	
    ->query;      	 	 	 
    $zbp->Config('ytecn_pjax')->snum=$sql[0]['num'];    		 		   
    $ztime = strtotime(date("Y-m"));        	   
    $sql = $zbp->db->sql->get()     		 			 
    ->select($zbp->table['Post'])    	  	  	 
    ->where(array('=', 'log_Type', "0"))    	 		 		 
    ->where(array('=', 'log_Status', "0"))    			  	  
    ->where(array('>', 'log_PostTime', $ztime))    		 		 	 
    ->count(array('log_ID'=>'num'))     	  	 		
    ->query;      	 		 	
    $zbp->Config('ytecn_pjax')->ynum=$sql[0]['num'];    		   			
    $today = date('Y-m-d');     		    	
    $dayOfWeek = date('N', strtotime($today));
    $daysToMonday = $dayOfWeek - 1;    		 		  	
    $ztime = strtotime("-$daysToMonday days", strtotime($today));    		 					
    $sql = $zbp->db->sql->get()    	  		  	
    ->select($zbp->table['Post'])      	   	 
    ->where(array('=', 'log_Type', "0"))    	 	 	 	 
    ->where(array('=', 'log_Status', "0"))    	 	     
    ->where(array('>', 'log_PostTime', $ztime))    	  		  	
    ->count(array('log_ID'=>'num'))    	 	 		 	
    ->query;     	    	 
    $zbp->Config('ytecn_pjax')->wnum=$sql[0]['num'];    		 	 	  
    $ztime = strtotime(date("Y-m-d"));     	 	 		 
    $sql = $zbp->db->sql->get()     	 		 		
    ->select($zbp->table['Post'])    			  	  
    ->where(array('=', 'log_Type', "0"))      	  	 	
    ->where(array('=', 'log_Status', "0"))      	     
    ->where(array('>', 'log_PostTime', $ztime))     	      
    ->count(array('log_ID'=>'num'))     			 			
    ->query;    	  	 	 	
    $zbp->Config('ytecn_pjax')->dnum=$sql[0]['num'];    	  		 		
    $zbp->SaveConfig('ytecn_pjax');    	 			 	 
}      		 		 
    	 			   
function ytecn_pjax_Category_Edit_Response() {
    global $zbp,$cate;
    echo '
    <p><span class="title">seo标题:</span><br /><input style="width:98%;" size="40" name="meta_tc_title" type="text" value="'.$cate->Metas->tc_title.'"></p>
    <p><span class="title">seo关键词:</span><br /><input style="width:98%;" size="40" name="meta_tc_keywords" type="text" value="'.$cate->Metas->tc_keywords.'"></p>
    <p><span class="title">seo描述:</span><br /><input style="width:98%;" size="40" name="meta_tc_description" type="text" value="'.$cate->Metas->tc_description.'"></p>';
}

function ytecn_pjax_Tag_Edit_Response() {     		     
    global $zbp,$tag;
    echo '
    <p><span class="title">seo标题:</span><br /><input style="width:98%;" size="40" name="meta_tc_title" type="text" value="'.$tag->Metas->tc_title.'"></p>
    <p><span class="title">seo关键词:</span><br /><input style="width:98%;" size="40" name="meta_tc_keywords" type="text" value="'.$tag->Metas->meta_tc_keywords.'"></p>
    <p><span class="title">seo描述:</span><br /><input style="width:98%;" size="40" name="meta_tc_description" type="text" value="'.$tag->Metas->tc_description.'"></p>';
}

function ytecn_pjax_Member_Edit_Response() {
    global $zbp,$member;
    echo '
    <p><span class="title">seo标题:</span><br /><input style="width:98%;" size="40" name="meta_tc_title" type="text" value="'.$member->Metas->tc_title.'"></p>
    <p><span class="title">seo关键词:</span><br /><input style="width:98%;" size="40" name="meta_tc_keywords" type="text" value="'.$member->Metas->meta_tc_keywords.'"></p>
    <p><span class="title">seo描述:</span><br /><input style="width:98%;" size="40" name="meta_tc_description" type="text" value="'.$member->Metas->tc_description.'"></p>';
}      		   	
    		    	 
function InstallPlugin_ytecn_pjax() {      	 			 
	global $zbp;      	 		  
}
function UninstallPlugin_ytecn_pjax() {}

function ytecn_pjax_SubMenu($action){
    $array = array(     		  	 	
        array('action' => 'index','url' => 'index.php','target' => '_self','float' => 'left','title' => '插件说明'),
        array('action' => 'set','url' => 'set.php','target' => '_self','float' => 'left','title' => '配置中心'),
        array('action' => 'seo','url' => 'seo.php','target' => '_self','float' => 'left','title' => 'SEO配置'),
    );     	  	 	 
    $str = '';     	 	  	 
    $template = '<a href="$url" target="$target"><span class="m-$float$light">$title</span></a>';
    for ($i = 0; $i < count($array); $i++) {
        $str .= $template;
        $str = str_replace('$url', $array[$i]['url'], $str);
        $str = str_replace('$target', $array[$i]['target'], $str);
        $str = str_replace('$float', $array[$i]['float'], $str);
        $str = str_replace('$title', $array[$i]['title'], $str);
        $str = str_replace('$light', ($action == $array[$i]['action'] ? ' m-now' : ''), $str);
    }
    return $str;
}

function ytecn_pjax_Admin_SiteInfo_SubMenu(&$m){
    global $zbp;    		 	  		
    $m[] = MakeTopMenu("root", '主题配置', $zbp->host . "zb_users/theme/ytecn_pjax/main/index.php", "", "ytecn_pjax", "icon-grid-1x2-fill");
}

function ytecn_pjax_edit_response() {
    global $zbp,$article;
    if ($_GET['act'] == 'PageEdt') return;
    echo "<script type=\"text/javascript\" src=\"{$zbp->host}zb_users/theme/ytecn_pjax/script/upload.js\"></script>";    			  			
    echo '<style type="text/css">.ytecn_input_img{width:40%!important;}.ytecn_upimgbutton{ width:120px;}.ytecn_upimg{width:100px;vertical-align: middle;}</style>';     		   		
    echo '<table border="1" class="tableFull tableBorder">';
    echo '<tr class="p_block" data-ret="ytecn_pjax">
	<td colspan="3" style="text-align: center;">+++++ 主题自带配置 +++++</td>
    </tr>';
    echo '<tr style="display: none;" class="p_none_ytecn_pjax">
	<td class="td30"><p align=\'left\'><b>缩略图</b></p></td>
	<td><input name="meta_pic" id="edtTitle" type="text" class="ytecn_input_img" value="' . $article->Metas->pic . '" /><input type="button" class="ytecn_upimgbutton" value="选择图片"><img class="ytecn_upimg" src="' . $article->Metas->pic. '"></td>
    </tr>';
    if($zbp->Config('ytecn_pjax')->seozdy){
        echo '<tr style="display: none;" class="p_none_ytecn_pjax">
        <td class="td30"><p align=\'left\'><b>seo标题：</b></p></td>
        <td><input type="text" name="meta_tc_title" value="'.htmlspecialchars($article->Metas->tc_title).'" style="width:89%;" /></td>
        </tr>';
        echo '<tr style="display: none;" class="p_none_ytecn_pjax">
        <td class="td30"><p align=\'left\'><b>seo关键词：</b></p></td>
        <td><input type="text" name="meta_tc_keywords"value="'.htmlspecialchars($article->Metas->tc_keywords).'" style="width:89%;" /></td>
        </tr>';
        echo '<tr style="display: none;" class="p_none_ytecn_pjax">
        <td class="td30"><p align=\'left\'><b>seo描述：</b></p></td>
        <td><input type="text" name="meta_tc_description" value="'.htmlspecialchars($article->Metas->tc_description).'" style="width:89%;" /></td>
        </tr>';
    }      	 		   	 				 	
    echo '</table>';
    echo '
    <script>$(document).on("click", ".p_block", function() {var e = $(this).attr("data-ret");$(".p_none_"+e).toggle();});

      $(function() {
        $(".newbtn1").click(function() {
          $(".sortbox1").append(itemStr1);
        });
      });
      $("#article_sort1").on("click", ".nciaer_delbtn", function() {
        $(this).parent().remove();
      })
    </script>';
}    	 		 		 
    	   	 		
function ytecn_pjax_post_get($post,$name){
    global $zbp;
    switch ($name) {
        case 'Img':
            $ac=$post->GetData()['Meta'];
            $ad=Unserialize($ac);    		     	
            if(empty($ad['pic'])){
                $imgs=GetImagesFromHtml($post->Content);
                if(empty($imgs['0'])){     		 	 		
                    $pic=$zbp->Config('ytecn_pjax')->proimg;
                }else{    	   			 
                    $pic=$imgs['0'];
                }
            }else{
                $pic=$ad['pic'];
            }    	  	 		 
            $pic = str_replace('{#ZC_BLOG_HOST#}', $zbp->host, $pic);
            $GLOBALS['hooks']['Filter_Plugin_Post_Get']['ytecn_pjax_post_get']=PLUGIN_EXITSIGNAL_RETURN;
            return $pic;
        case 'Intro':
            $intro=TransferHTML($post->Content,'[nohtml]');
            $intro=str_replace("&nbsp;","",$intro);
            $intro=str_replace('style=""',"",$intro);
            $intro=SubStrUTF8($intro,45)."...";
            $GLOBALS['hooks']['Filter_Plugin_Post_Get']['ytecn_pjax_post_get']=PLUGIN_EXITSIGNAL_RETURN;      				  
            return $intro;    	 		    
        default:      	 	 		
            break;         	 	
    }          		
}     		 	  	
     				  	
function ytecn_pjax_PostArticle_Core(&$article) {
    global $zbp;
    $intro=preg_replace('/[\r\n\s]+/', '', trim((TransferHTML($article->Content,'[nohtml]'))));
    $intro=str_replace("&nbsp;","",$intro);
    $intro=str_replace('style=""',"",$intro);
    $intro=SubStrUTF8($intro,45)."...";
    $article->Intro=$intro;
    $content=$article->Content;
    $content = preg_replace("/data-src=.+?['|\"]/i",'',$content);
    $article->Content=$content;
}

function ytecn_pjax_set(&$template){    		 	   	
    global $zbp;      		 		 
    $template['ytecn']=$zbp->Config('ytecn_pjax');
}